package cn.lr.org.controller;

import cn.lr.basic.util.AjaxResult;
import cn.lr.basic.util.PageList;
import cn.lr.org.domain.Detail;
import cn.lr.org.query.DetailQuery;
import cn.lr.org.service.IDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
public class DetailController {
    @Autowired
    private IDetailService detailService;

    /**
     * 查询单个--restful风格
     * @param id
     * @return
     */
//    @GetMapping("/{id}")
//    public Detail queryById(@PathVariable("id") Long id){
//        return detailService.loadById(id);
//    }

    @PostMapping("/{id}")
    public List<Detail> queryByTypeId(@PathVariable("id") Long id){
        return detailService.queryByTypeId(id);
    }

    /**
     * 查询所有--restful风格
     * @return
     */
    @GetMapping
    public List<Detail> queryAll(){
        return detailService.loadAll();
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable("id") Long id) {
        try {
            detailService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids) {
        try {
            detailService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
           return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

    @PostMapping
    public PageList<Detail> queryPage(@RequestBody DetailQuery query){
        return detailService.queryPage(query);
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Detail detail) {
        try {
            if(detail.getId() != null){
                detailService.update(detail);
            } else
                detailService.save(detail);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me()
                    .setSuccess(false)
                    .setMessage("系统繁忙，请稍后重试！！！");
        }
    }

}
